﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using Citi.ReportManager.Domain;
using Citi.SecurityHelper;

namespace Citi.ReportManager.WebUi.Models
{
    public class HighlightedReportModel
    {
        public int Id { get; set; }
        public string Soeid { get; set; }
        public int CategoryId { get; set; }
        public CategoryModel Category { get; set; }
        public string ReportPath { get; set; }
        public DateTime RequestDate { get; set; }
        public string Icon { get; set; }
        public bool ShowRequestDate { get; set; }

        public string ReportName
        {
            get
            {
                var reportName = !string.IsNullOrWhiteSpace(Path.GetExtension(Path.GetFileNameWithoutExtension(ReportPath)))
                       ? Path.GetFileNameWithoutExtension(ReportPath)
                       : ReportPath;

                return Path.GetFileName(reportName);
            }
        }

        public string ReportFullPath
        {
            get
            {
                var filesPath = ConfigurationManager.AppSettings["FilesPath"];

                return Path.Combine(filesPath, CategoryId.ToString(), ReportPath);
            }
        }

        public decimal Size
        {
            get
            {
                using (ImpersonateCitiUser.GetImpersonateUser(@"lac_bra_ftools", @"lac\lac_bra_ftools", "Ferrari3k10"))
                    return decimal.Divide(new FileInfo(ReportFullPath).Length, 1048576);
            }
        }

        public bool Valid
        {
            get
            {
                try
                {
                    using (ImpersonateCitiUser.GetImpersonateUser(@"lac_bra_ftools", @"lac\lac_bra_ftools", "Ferrari3k10"))
                        return File.Exists(ReportFullPath);
                }
                catch
                {
                    return false;
                }
            }
        }

        public HighlightedReportModel CreateModel(HightlightedReport domain)
        {
            return new HighlightedReportModel
            {
                CategoryId = domain.ReportFolderId,
                Category = new CategoryModel().CreateModel(domain.Category),
                Id = domain.Id,
                ReportPath = domain.ReportPath,
                RequestDate = domain.Date,
                Soeid = domain.Soeid
            };
        }

        public IEnumerable<HighlightedReportModel> CreateModel(IEnumerable<HightlightedReport> domain)
        {
            return domain.Select(CreateModel);
        }
    }
}
